var app = new Vue({
	el: '#app',
	data: {
		oldpassword: '',
		newpassword: '',
		newname: '',
		adminMsg: {
			isLogin: false,
			id: null,
			account: null,
			name: null,
			headimg: null
		},
		addModuleName: '',
		renameModuleName: '',
		renameModuleId: '',
		originalName: '',
		moduleMsg: []
	},
	computed: {
		ableModule: function(){
			var ret = [];
			for (var i = 0; i<this.moduleMsg.length; i++) {
				if (this.moduleMsg[i].able == 1) {
					ret.push(this.moduleMsg[i])
				}
			}
			return ret;
		},
		unableModule: function(){
			var ret = [];
			for (var i = 0; i<this.moduleMsg.length; i++) {
				if (this.moduleMsg[i].able == 0) {
					ret.push(this.moduleMsg[i])
				}
			}
			return ret;
		}
	},
	methods: {
		changePassword: function(){
			var self = this;
			var id = self.adminMsg.id;
			var oldpassword = self.oldpassword;
			var newpassword = self.newpassword;
			var data = {
				"id": id,
				"oldpassword": oldpassword,
				"newpassword": newpassword
			}
			$.post("/api/admin/changePassword", data, function(data,status){
				if (data.success == "ok") {
					$("#passwordModal").modal("hide");
					swal("修改成功","","success");
				} else {
					swal("修改失败",data.msg, "error");
				}
			})
		},
		changeName: function(){
			var self = this;
			var id = self.adminMsg.id;
			var name = self.newname;
			var data = {
				"id": id,
				"name": name
			}
			$.post("/api/admin/setAdmin", data, function(data,status){
				if (data.success == "ok") {
					$("#nameModal").modal("hide");
					self.getLoginStatus();
					swal("修改成功","","success");
				} else {
					swal("修改失败",data.msg, "error");
				}
			})
		},
		logout: function(){
			var self = this;
			swal({ 
				title: "确定退出吗？", 
				type: "warning",
				showCancelButton: true, 
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定", 
  				cancelButtonText: "取消"
			},function(){
				$.post("/api/admin/logout",{},function(data,status){
					if (data.success = "ok") {
						self.getLoginStatus();
					}
				});
			});
		},
		getLoginStatus: function(){
			var self = this;
			$.get('/api/admin/isLogin',function(data,status){
				if (data.success == "ok") {
					self.adminMsg.isLogin = true;
					self.adminMsg.name = data.result.name;
					self.adminMsg.id = data.result.id;
					self.adminMsg.account = data.result.account;
					self.adminMsg.headimg = data.result.headimg;
				} else {
					self.adminMsg.isLogin = false;
					window.location.href = "/admin/login.html"
				}
			})
		},
		showAddModule: function(){
			$("#addModuleModal").modal('show');
		},
		showRenameModule: function(name,id){
			this.originalName = name;
			this.renameModuleId = id;
			$("#renameModuleModal").modal('show');
		},
		addModule: function(){
			var self = this;
			var name = this.addModuleName;
			var data = {"name": name};
			$.post("/api/admin/createModule",data,function(data,status){
				if (data.success == "ok") {
					$("#addModuleModal").modal('hide');
					self.addModuleName = "";
					swal("添加成功","","success");
					self.getModule();
				} else {
					swal("添加失败",data.msg,"error");
				}
			});
		},
		getModule: function(){
			var self = this;
			$.get('/api/admin/getModule',function(data,status){
				if (data.success == "ok") {
					self.moduleMsg = data.result;
				} else {
					swal("发生错误","可能网络出现了问题...","error");
				}
			})
		},
		deleteModule: function(id){
			var self = this;
			var data = {"able": 0,"id":id};
			swal({ 
				title: "确定停用吗？", 
				type: "warning",
				showCancelButton: true, 
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定", 
  				cancelButtonText: "取消"
			},function(){
				$.post('/api/admin/setModule',data,function(data,status){
					if (data.success == "ok") {
						swal("设置停用成功","","success");
						self.getModule();
					} else {
						swal("发生错误","可能网络出现了问题...","error");
					}
				})
			});
		},
		recoverModule: function(id){
			var self = this;
			var data = {"able": 1,"id":id};
			swal({ 
				title: "确定启用吗？", 
				type: "warning",
				showCancelButton: true, 
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定", 
  				cancelButtonText: "取消"
			},function(){
				$.post('/api/admin/setModule',data,function(data,status){
					if (data.success == "ok") {
						swal("启用成功","","success");
						self.getModule();
					} else {
						swal("发生错误","可能网络出现了问题...","error");
					}
				})
			});
		},
		renameModule: function(){
			var self = this;
			var name = String(self.renameModuleName);
			var id = Number(self.renameModuleId);
			var data = {"name":name,"id":id};
			if (name == undefined || name == "") {
				swal("不能为空","","error");
				return ;
			}
			swal({ 
				title: "确定改名吗？", 
				type: "warning",
				showCancelButton: true, 
				confirmButtonColor: "#DD6B55",
				confirmButtonText: "确定", 
  				cancelButtonText: "取消"
			},function(){
				$.post('/api/admin/setModule',data,function(data,status){
					if (data.success == "ok") {
						swal("改名成功","","success");
						$("#renameModuleModal").modal('hide');
						self.renameModuleName = "";
						self.getModule();
					} else {
						swal("发生错误","可能网络出现了问题...","error");
					}
				})
			});
		}

	},
	mounted: function(){
		this.getLoginStatus();
		this.getModule();
	}
})
